<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$THEME->addBreadcrumb($this->adminUrl.'/sort-pages/', $this->lang('ap_title_sort-pages'));

$pagesRecursive = JFX::getPageTree(0, $USER->workspace);

$tree = '<ul id="jfx-sort-page-tree">'.JFX_SortPage_MakeTree($pagesRecursive).'</ul>';

$SMARTY->assign('sortTree', $tree);

$VIEW->addTemplate('main', 'admin/sort-pages.tpl');

$SMARTY->assign('showPages', false);




$VIEW->addJs('
$(document).ready(function(){
     var sortableStop = function(){

        var sorted = $("#jfx-sort-pages").sortable("serialize");
        //alert(sorted);
        //$("#jfx-sort-pages").css("display", "none");
        //$("#jfx-workspace-inner").get(0).innerHTML = \'<div align="center"><img src="'.$CONFIG->imageUrl.'/admin/loadingAnimation.gif" alt="Loading..." /></div>\';
        $.post("'.$CONFIG->adminUrl.'/sort-pages/?page='.$pageid.'&sorted=1", sorted, function(){});
    }
    $("#jfx-sort-pages").sortable({cursor : "pointer", stop : sortableStop });
    $("#jfx-sort-page-tree").treeview({
        persist: "location",
        collapsed: true,
        unique: true
    });
});
');




if(get('page')!=''){
    $pageid = (int) get('page');
    if($DB->countRows($CONFIG->dbprefix.'pages', "id = '{$pageid}'")==0){
        JFX::addError('Invalid page selected');
        JFX::redirect($CONFIG->adminUrl.'/sort-pages');
    }
}else{
    return;
}

// now have a valid page id

$SMARTY->assign('showPages', true);


if(get('sorted')==1){
    // update the sort order
    $DB->showOutput(true);
    $count = 0;
    foreach($_POST['page'] as $sortOrder => $id){
        $id = (int) $id;
        $DB->update($CONFIG->dbprefix.'pages', array('sorting'=>$count), "id = '{$id}'");
        $count++;
    }
    exit;
    while(ob_get_level()>0) ob_end_clean();
    exit;
}





$pageTitle = $LANG->getContent('core', 'page_title', $pageid);
$SMARTY->assign('pageTitle', $pageTitle);


$pages = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}pages WHERE parent_id = '{$pageid}' ORDER BY sorting ASC");

foreach($pages as $k=>$v){
    $pages[$k]['title'] = $LANG->getContent('core', 'page_title', $v['id']);
}

$SMARTY->assign('pages', $pages);


































function JFX_SortPage_MakeTree($arr){
    $CONFIG = JFX::registry('config');
    $LANG = JFX::registry('lang');
    $returnString = '';
    foreach($arr as $k=>$v){

        $link = '<a href="'.$CONFIG->adminUrl.'/sort-pages/?page='.$v['id'].'">'.$LANG->getContent('core', 'page_title', $v['id']).'</a>';
        if(isset($v['subpages']) && count($v['subpages'])>0){
            $subpages = '<ul>'.JFX_SortPage_MakeTree($v['subpages']).'</ul>';
        }else{
            $subpages = '';
        }
        $returnString .= '<li>'.$link.$subpages.'</li>';
    }

    return $returnString;
}





